﻿using UnityEngine;
using System.Collections;

public class CharacterFactory : MonoBehaviour {

	//klasa atrybutów postaci
	public class attrib { 
		public int faceNmb = 0;
		public int clothNmb = 0;
		public int hairNmb = 0;
		public int bandanaNmb = 0;
		public int hoodNmb = 0;
		public bool sex;
	}

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}

	/** LOSOWANIE ATRYBUTÓW POSTACI: UBRANIA, TWARZY, NAKRYCIA GŁOWY/WŁOSÓW*/
	public static attrib rollForAttributes(bool sex) {
		attrib newAttrib = new attrib ();
		newAttrib.sex = sex;
		
		/**LOSOWANIE TWARZY*/
		//szansa na podstawową twarz == 30%
		if (Random.Range (0f, 100f) > 30f) {
			if (sex) 
				//ilość prefabów: 17, zakres 1-17
				newAttrib.faceNmb = Random.Range(0,17)+1;
			else 
				//ilość prefabów: 11, zakres 1-11
				newAttrib.faceNmb = Random.Range(0,11)+1;
		}
		
		/**LOSOWANIE UBRANIA*/
		//szansa na losowe ubranie == 100%
		if (sex) 
			//ilość prefabów: 10, zakres 1-10
			newAttrib.clothNmb = Random.Range(0,10)+1;
		else
			//ilość prefabów: 10, zakres 1-10
			newAttrib.clothNmb = Random.Range(0,10)+1;
		
		/**LOSOWANIE GŁOWY (WŁOSÓW/CZAPKI) */
		//szansa na podstawową głowę == 10%
		if (Random.Range (0f, 100f) > 10f) {
			//szansa na fryzurę == 70 %
			if (Random.Range (0f, 100f) > 30f) {
				if(sex)
					//ilość prefabów włosów męskich: 36, zakres 1-36
					newAttrib.hairNmb = Random.Range(0,36)+1;
				else
					//ilość prefabów włosów żeńskich: 26, zakres 1-26
					newAttrib.hairNmb = Random.Range(0,26)+1;
				//szansa na nakrycie głowy == 30%
			} else {
				//NAKRYCIE GŁOWY
				//czapka czy bandana?
				if (Random.Range (0f, 100f) > 50f) 
					//BANDANA 
					//ilość prefabów TAKIE SAME DLA OBU PŁCI: 6, zakres 1-6
					newAttrib.hoodNmb = Random.Range(0,6)+1;
				else
					//CZAPKA/KAPTUR
					//ilość prefabów: 14, zakres 1-14
					newAttrib.hoodNmb = Random.Range(0,14)+1;
			}
		}
		return newAttrib;
	}

	public static void loadFace(bool sex, Transform parent, int faceNmb) 
	{
		if(faceNmb > 0) {
			GameObject newFace;
			if (sex) 
				newFace = Instantiate(
					Resources.Load<GameObject>("Characters/Prefabs/Man/Faces/face" + faceNmb)) as GameObject;
			else 
				newFace = Instantiate(
					Resources.Load<GameObject>("Characters/Prefabs/Woman/Faces/face" + faceNmb)) as GameObject;
			//ustawienie parenta
			newFace.transform.parent = parent;
			newFace.transform.localScale = new Vector3(1f,1f,1f);
			newFace.transform.localPosition = new Vector3(0f,0f,-0.001f);
		}
	}
	
	public static void loadCloth(bool sex, Transform parent, int clothNmb) 
	{
		GameObject newCloth;
		if (sex) 
			newCloth = Instantiate(
				Resources.Load<GameObject>("Characters/Prefabs/Man/Clothes/cloth" + clothNmb)) as GameObject;
		else 
			newCloth = Instantiate(
				Resources.Load<GameObject>("Characters/Prefabs/Woman/Clothes/cloth" + clothNmb)) as GameObject;
		
		//ustawienie parenta
		newCloth.transform.parent = parent;
		//ustawienie parametrów
		newCloth.transform.localScale = new Vector3(1f,1f,1f);
		newCloth.transform.localPosition = new Vector3(0f,0f,-0.001f);
	}
	
	public static void loadHead(bool sex, Transform parent, int hairNmb, int bandanaNmb, int hoodNmb) 
	{
		
		if (hairNmb > 0) {
			GameObject newHead;
			if(sex)
				newHead = Instantiate(
					Resources.Load<GameObject>("Characters/Prefabs/Man/Head/hair" + hairNmb)) as GameObject; 
			else
				newHead = Instantiate(
					Resources.Load<GameObject>("Characters/Prefabs/Woman/Head/hair" + hairNmb)) as GameObject;
			//ustawienie parenta
			newHead.transform.parent = parent;
			//ustawienie parametrów
			newHead.transform.localScale = new Vector3(1f,1f,1f);
			newHead.transform.localPosition = new Vector3(0f,0f,-0.001f);
		} else if (bandanaNmb > 0) {
			GameObject newHead;
			if(sex)
				newHead = Instantiate(
					Resources.Load<GameObject>("Characters/Prefabs/Man/Head/bandana" + bandanaNmb)) as GameObject; 
			else
				newHead = Instantiate(
					Resources.Load<GameObject>("Characters/Prefabs/Woman/Head/bandana" + bandanaNmb)) as GameObject;
			//ustawienie parenta
			newHead.transform.parent = parent;
			//ustawienie parametrów
			newHead.transform.localScale = new Vector3(1f,1f,1f);
			newHead.transform.localPosition = new Vector3(0f,0f,-0.001f);
		} else if(hoodNmb > 0) {
			GameObject newHead;
			if(sex)
				newHead = Instantiate(
					Resources.Load<GameObject>("Characters/Prefabs/Man/Head/hood" + hoodNmb)) as GameObject; 
			else
				newHead = Instantiate(
					Resources.Load<GameObject>("Characters/Prefabs/Woman/Head/hood" + hoodNmb)) as GameObject;
			//ustawienie parenta
			newHead.transform.parent = parent;
			//ustawienie parametrów
			newHead.transform.localScale = new Vector3(1f,1f,1f);
			newHead.transform.localPosition = new Vector3(0f,0f,-0.001f);
		}
	}
}
